<template>
  <div id="login-container">
    <h3 style="margin-bottom:10px">欢迎使用</h3>
    <el-input placeholder="请输入用户名" v-model="username" class="loginInput">
      <i slot="prefix" class="el-input__icon el-icon-user"></i>
    </el-input>
    <p class="tip"></p>
    <el-input
      placeholder="请输入密码"
      v-model="password"
      show-password
      class="loginInput"
    >
      <i slot="prefix" class="el-input__icon el-icon-lock"></i>
    </el-input>
    <p class="tip"></p>
    <el-button type="primary" class="loginBtn" @click="handleLogin"
      >登录</el-button
    >
  </div>
</template>

<script>
import { login } from "../api/index";

export default {
  name: "login",
  data() {
    return {
      username: "",
      password: ""
    };
  },
  methods: {
    handleLogin() {
      if (this.username == "") {
        this.$message.error("请输入用户名");
        return;
      }
      if (this.password == "") {
        this.$message.error("请输入密码");
        return;
      }
      login(this.username, this.password).then(res => {
        console.log(res.data);
        if (res.data.code == 0) {
          this.$message({
            message: "登陆成功",
            type: "success",
            center: true,
            onClose: () => {
              this.$router.push("/workspace");
            }
          });
        } else {
          if (res.data.code != 200) {
            this.$message({
              type: "error",
              message: res.data.msg,
              center: true
            });
          }
        }
      });
    }
  }
};
</script>

<style scoped>
#login-container {
  width: 300px;
  height: 200px;
  padding: 20px 0;
  background-color: #fff;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  display: flex;
  flex-direction: column;
  justify-content: space-around;
  align-items: center;
  border-radius: 10px;
}

.loginInput,
.loginBtn {
  width: 85%;
}

.tip {
  flex: 1;
  font-size: 10px;
  align-self: start;
  margin-left: 25px;
  color: #ec7c7c;
}
</style>
